import React, { FC } from 'react';
import { Redirect } from 'umi';

const Auth: FC<any> = props => {
  const nowPath = props.routes[0].routes.filter(
    (item: any) => item.path === props.location.pathname,
  );
  const isLogin =
    localStorage.getItem('token') && localStorage.getItem('username');
  if (nowPath.length === 1 && nowPath[0].auth && !isLogin) {
    return <Redirect to={`/login?from=${props.location.pathname}`} />;
  } else {
    return <div>{props.children}</div>;
  }
};

export default Auth;
